<?php

namespace app\api\model\kuyou\applet;

use app\api\controller\kuyou\applet\Commons;
use think\Model;
use think\Db;
use Exception;
class Enclosure extends Model
{

    // 表名
    protected $name = 'kuyou_enclosure';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList($where,$page,$limit,$list_type,$sort_type,$status_type){
        if(1<=$status_type && $status_type<=6){
            $enclosure_id = Db::name('kuyou_enclosure_log')->where(['type'=>$status_type])->column('enclosure_id');
            $where['id'] = ['in',$enclosure_id];
        }
        if($list_type==1){
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->group('upload_date')->order('upload_date',$sort_type)->field("upload_date")->select())->toArray();
            foreach ($list as $list_k=>$list_v){
                $where['upload_date'] = $list_v['upload_date'];
                $lists = collection(self::where(function ($query) use ($where) {
                    $query->where($where);
                })->field("id,image,upload_date,FROM_UNIXTIME(createtime,'%Y-%m-%d') as createtime")->select())->toArray();
                $list[$list_k]['list'] = $lists;
            }
            $count = sizeof($list);
        }else{
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }
            if($pageStatus){
                $list = collection(self::where(function ($query) use ($where) {
                    $query->where($where);
                })->page($page,$limit)->order('upload_date',$sort_type)->field("id,image,upload_date,FROM_UNIXTIME(createtime,'%Y-%m-%d') as createtime")->select())->toArray();
            }else{
                $list = collection(self::where(function ($query) use ($where) {
                    $query->where($where);
                })->order('upload_date',$sort_type)->field("id,image,upload_date,FROM_UNIXTIME(createtime,'%Y-%m-%d') as createtime")->select())->toArray();
            }
            $count = self::where(function ($query) use ($where) {
                $query->where($where);
            })->count();
        }
        return ['list'=>$list,'count'=>$count];
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'image|原始单据'=>'require',
                'type|单据类型'=>'require|in:1,2',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $detail['upload_date'] = date('Y-m-d');
                $detail['createtime'] = time();
                $detail['updatetime'] = time();

                $res = false;
                Db::startTrans();
                try {
                    $res = (new Enclosure())->allowField(true)->save($detail);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    return ['status'=>500,'msg'=>$e->getMessage()];
                }
                if($res){
                    return ['status'=>200,'msg'=>'添加成功'];
                }else{
                    return ['status'=>500,'msg'=>'添加失败'];
                }
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public static function delDetail($id){
        if(empty($id)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $ids = explode(',',$id);
            $list = (new Enclosure())->where(['id'=>['in',$ids]])->select();
            $log = Db::name('kuyou_enclosure_log')->where(['enclosure_id'=>['in',$ids]])->find();
            if($log){
                return ['status'=>500,'msg'=>'原始单据已关联业务单据，不可删除！'];
            }
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += Db::name('kuyou_enclosure')->where(['id'=>$v['id']])->delete();
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                return ['status'=>500,'msg'=>$e->getMessage()];
            }
            if($count){
                return ['status'=>200,'msg'=>$count];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }
}
